﻿using System;
using System.Data;
using System.Diagnostics;
using Lari.Configuration;
using PerformanceCounter = Lari.Services.Data.PerformanceCounter;

namespace Lari.DataAccess
{
    public partial class LariDbContext
    {
        private readonly DateTime _startTime = DateTime.Now;
        public LariDbContext(bool enableProxyCreation = false) : base("name=LariDbContext")
        {
            Configuration.ProxyCreationEnabled = enableProxyCreation;
        }
        protected override void Dispose(bool disposing)
        {
            if (ApplicationConfig.LogExecutionTime)
            {
                var timeDiff = (DateTime.Now - _startTime).Milliseconds;
                var st = new StackTrace(2);
                var declaringType = st.GetFrame(0).GetMethod().DeclaringType;
                if (declaringType != null)
                {
                    var pc = new PerformanceCounter { CreatedOn = DateTime.Now, ClassName = declaringType.ToString(), ExecutionTime = timeDiff, FunctionName = st.GetFrame(0).GetMethod().ToString() };
                    Entry(pc).State = EntityState.Added;
                    SaveChanges();
                }
               
            }
            base.Dispose(disposing);
            GC.SuppressFinalize(this);

        }

        
    }
}